package rpg.visual;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;

import pgui.visual.PImage;
import rpg.map.Creature;
import rpg.map.Effect;
import rpg.map.MapObject;
import square.data.MapObjectData;
import square.data.SpriteData;

/**
 * 
 * @author Pedro Silva Moreira
 */
public abstract class VisualImage {

	public static final int DEFAULT_SQUARE_SIZE = 32;
	public static int SQUARE_SIZE = 32;

	// Graphics2D
	public static void opacity(Graphics2D g, float opacity) {
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
				opacity));
	}

	// Event
	public static void onPaint(Graphics2D g, MapObject object) {
		PImage image;

		// Effect
		if (object.getClass() == Effect.class) {
			Effect effect = (Effect) object;

			if (effect.getColor() != null) {
				g.setColor(effect.getColor());
			}
		}

		// Creature
		if (object.getClass() == Creature.class) {
			Creature creature = (Creature) object;

			double value = creature.walkPercent() * SQUARE_SIZE / 100;
			switch (object.getSide()) {
			case MapObjectData.SIDE_RIGHT:
				g.translate(value, 0);
				break;
			case MapObjectData.SIDE_DOWN:
				g.translate(0, value);
				break;
			case MapObjectData.SIDE_LEFT:
				g.translate(-value, 0);
				break;
			case MapObjectData.SIDE_UP:
				g.translate(0, -value);
				break;
			case MapObjectData.SIDE_RIGHT_UP:
				g.translate(value, -value);
				break;
			case MapObjectData.SIDE_RIGHT_DOWN:
				g.translate(value, value);
				break;
			case MapObjectData.SIDE_LEFT_UP:
				g.translate(-value, -value);
				break;
			case MapObjectData.SIDE_LEFT_DOWN:
				g.translate(-value, value);
				break;
			}
			
			image = (PImage) object
					.getData()
					.getSprite()
					.getFrame(
							(object.getSide() % 4) * 3
									+ ((Creature) object).walkState());
			
			g.translate(SQUARE_SIZE - image.getWidth(), SQUARE_SIZE
					- image.getHeight());			
			
			image.draw(g);
			
			g.translate(image.getWidth() - SQUARE_SIZE, image.getHeight() - SQUARE_SIZE);

			// Name
			String name = creature.getData().getName();
			g.setColor(Color.white);
			g.drawString(name, -g.getFontMetrics().stringWidth(name) / 2
					+ SQUARE_SIZE / 2, -12);

			opacity(g, 0.5f);

			// Health
			int healthPercent = creature.getData().getHealth() * 100
					/ creature.getData().getMaxHealth();
			int healthBorder = 1;
			int healthHeight = 4;

			g.setColor(Color.red);
			g.fillRect(healthBorder, -healthHeight - healthBorder - 2,
					(SQUARE_SIZE - healthBorder * 2) * healthPercent / 100,
					healthHeight);

			g.setColor(Color.black);
			g.drawRect(healthBorder, -healthHeight - healthBorder - 2,
					(SQUARE_SIZE - healthBorder * 2), healthHeight);


			opacity(g, 1);
		} else {
			SpriteData sprite = object.getData().getSprite();

			if (sprite.getAnimation() > 0) {
				if (sprite.getLastPaint() == null) {
					sprite.setLastPaint(System.currentTimeMillis());
				} else if (System.currentTimeMillis() - sprite.getLastPaint() > sprite
						.getAnimation()) {
					sprite.setLastPaint(System.currentTimeMillis());

					int nextFrame = (sprite.getCurrentFrame() + 1)
							% sprite.getFrames().size();
					sprite.setCurrentFrame(nextFrame);
				}
			}

			int frame = sprite.getCurrentFrame() + object.getSide() * 3;
			image = (PImage) sprite.getFrame(frame);
			g.translate(SQUARE_SIZE - image.getWidth(), SQUARE_SIZE
					- image.getHeight());
			image.draw(g);
		}
	}
}